package com.grantheath.erebus.view.transitionHandelers 
{
	import flash.display.DisplayObjectContainer;
	import flash.display.DisplayObject;
	import com.greensock.TweenLite;

	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.EventDispatcher;

	import mx.controls.Image;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;

	import com.grantheath.erebus.view.interfaces.IErebusTransitionHandeler;
	import com.grantheath.erebus.view.interfaces.IErebusPage;
	import com.grantheath.erebus.view.helpers.ErebusPageEvent;

	/**
	 * @author grantheath
	 */
	public class ErebusFlexScaleTransitionHandeler extends EventDispatcher implements IErebusTransitionHandeler 
	{
		private var _target : DisplayObject;
		private var _layer : DisplayObjectContainer;
		private var _snapshot : Image;
		private var _duration : Number;
		private var _outDuration : Number;
		private var _x : Number;
		private var _y : Number;
		private var _width : Number;
		private var _height : Number;
		private var _scale : Number;

		public function ErebusFlexScaleTransitionHandeler(target : DisplayObject, layer : DisplayObjectContainer)
		{
			_duration = 0.3;
			_outDuration = 0.3;
			_target = target;
			_layer = layer;
		}

		public function transitionIn() : void
		{
			_snapshot = new Image();
			_snapshot.smoothBitmapContent = true;
			_snapshot.visible = false;
			_target.visible = false;
			scale = 0;
			target.addEventListener(FlexEvent.CREATION_COMPLETE, onPageCreationComplete);
			_layer.addChild(_target);
		}

		public function transitionOut() : void
		{
			takeSnapshot();
			_target.visible = false;
			_snapshot.visible = true;
			TweenLite.to(this, outDuration, {scale : 0, onComplete : onTransitionOut});
		}
		
		private function takeSnapshot() : void
		{
			var snapshotData : BitmapData;
			var snapshotBitmap : Bitmap;
			var snapshotMatrix : Matrix;
			
			_x = _target.x;
			_y = _target.y;
			_width = _target.width;
			_height = _target.height;
			_snapshot.x = _x;
			_snapshot.y = _y;
			if(_width == 0){
				_width = 1;
			}
			if(_height == 0) {
				_height = 1;
			}
			snapshotMatrix = new Matrix();
			snapshotData = new BitmapData(_width, _height, true, 0x00000000);
			snapshotData.draw(_target, snapshotMatrix);
			snapshotBitmap = new Bitmap(snapshotData);
			_snapshot.source = snapshotBitmap;
		}

		//EVENT HANDELERS
		private function onPageCreationComplete(event : Event) : void
		{
			takeSnapshot();
			layer.addChild(_snapshot);
			_snapshot.visible = true;
			TweenLite.to(this, duration, {scale : 1, onComplete : onTransitionIn});
		}
		
		private function onTransitionIn() : void
		{
			_snapshot.visible = false;
			_target.visible = true;
			dispatchEvent(new ErebusPageEvent(ErebusPageEvent.TRANSITION_IN_COMPLETE));
		}

		private function onTransitionOut() : void
		{
			layer.removeChild(_snapshot);
			dispatchEvent(new ErebusPageEvent(ErebusPageEvent.TRANSITION_OUT_COMPLETE));
		}

		//GETTERS AND SETTERS
		public function get target() : DisplayObject
		{
			return _target;
		}
		
		public function get layer() : DisplayObjectContainer
		{
			return _layer;
		}

		public function get duration() : Number
		{
			return _duration;
		}

		public function get outDuration() : Number
		{
			return _outDuration;
		}

		public function set duration(duration : Number) : void
		{
			_duration = duration;
		}

		public function set outDuration(outDuration : Number) : void
		{
			_outDuration = outDuration;
		}
		
		public function get scale() : Number
		{
			return _scale;
		}
		
		public function set scale(scale : Number) : void
		{
			_snapshot.scaleX = scale;
			_snapshot.scaleY = scale;
			_snapshot.x = _x + ((_width - (_width * scale) ) / 2);
			_snapshot.y = _y + ((_height - (_height * scale)) / 2);
			_scale = scale;
		}
	}
}
